package GongNeng;

import GJ.ArrayList;
import Main.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.*;

public class Enter extends JFrame {

    private static JTextField textField;
    private static JTextField textField1;
    private static JTextField textField3;
    private static JTextField textField4;
    private boolean flag1=false,flag2;
    private JLabel label;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JButton button;
    private JButton button1;
    private int length=0;
    private static File file1;
    private static File file2;
    private static File file3;
    private static File file4;
    private static String str1;
    private static String str2;
    private static String str3;
    private static String str4;
    private static StringBuffer shuxu;
    private static int l;


    public static <S> void main(String args[]) throws IOException {
        Enter enter = new Enter();
        enter.setVisible(true);
    }


    public Enter(){
        super();
        setTitle("图书录入");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBounds(0,0,1200,800);
        panel.setLayout(null);
        getContentPane().add(panel);


        final JLabel label = new JLabel("书名：");
        label.setBounds(400,50,200,150);
        label.setFont(new Font("",Font.BOLD,30));
        panel.add(label);

        label1 = new JLabel("书序：");
        label1.setBounds(400,130,200,150);
        label1.setFont(new Font("",Font.BOLD,30));
        panel.add(label1);

        label2 = new JLabel("作者：");
        label2.setBounds(400,210,200,150);
        label2.setFont(new Font("",Font.BOLD,30));
        panel.add(label2);

        label3 = new JLabel("出版社：");
        label3.setBounds(368,290,200,150);
        label3.setFont(new Font("",Font.BOLD,30));
        panel.add(label3);

        textField = new JTextField();
        textField.setBounds(475,105,300,45);
        textField.addFocusListener(new TextFieldFocus());
        panel.add(textField);

        textField1 = new JTextField();
        textField1.setBounds(475,185,300,45);
        textField1.addFocusListener(new TextFieldFocus());
        panel.add(textField1);

        textField3 = new JTextField();
        textField3.setBounds(475,265,300,45);
        textField3.addFocusListener(new TextFieldFocus());
        panel.add(textField3);

        textField4 = new JTextField();
        textField4.setBounds(475,345,300,45);
        textField4.addFocusListener(new TextFieldFocus());
        panel.add(textField4);

        button1 = new JButton("返回");
        button1.setBounds(680,450,100,50);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String buttonName = e.getActionCommand();
                if (buttonName.equals("返回")){
                    dispose();
                    new Main().setVisible(true);
                }
            }
        });
        panel.add(button1);
        button = new JButton("确认");
        button.setBounds(480,450,100,50);
        panel.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String str1 = "书名：" + textField.getText() + ";";
                String str5 = "书序：" + textField1.getText();
                String str2 = "书序：" + textField1.getText() + ";";
                String str3 = "作者：" + textField3.getText() + ";";
                String str4 = "出版社：" + textField4.getText() + ";";

                    //从文件中读取信息到ArrayList中如果书序相同则让用户重新输入
                    File file2 = new File("D://Books");
                    try {
                    BufferedReader bufferedReader;
                    bufferedReader=new BufferedReader(new FileReader(file2));
                    ArrayList arrayList = new ArrayList(100);
                    String line; String zhen = null; String[] song;
                    while ((line = bufferedReader.readLine())!= null) {
                        song = line.split(";");
                       for (int i=0;i<song.length;i++) {
                           arrayList.addBooks(song[i]);
                       }zhen = arrayList.get(0);
                        if ( zhen.equals(str5)) { flag2=true;break;
                        }else{ flag1 = false;break;
                        }
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                if (flag2){
                    JOptionPane.showMessageDialog(null,"书序重复，请重新输入！");
                    dispose();
                    new Enter().setVisible(true);
                }else {
                            //如果书序相同不进行录入，否则，录入
                            File file = new File("D://Books");
                            BufferedWriter bufferedWriter = null;
                            try {
                            bufferedWriter=new BufferedWriter(new FileWriter(file, true));
                            ArrayList arrayList2 = new ArrayList();
                            arrayList2.addBooks(str2);arrayList2.addBooks(str1);
                            arrayList2.addBooks(str3);arrayList2.addBooks(str4);
                            for (int i = 0; i < arrayList2.size; i++) {
                            String[] song1 = new String[100];
                            song1[i] = arrayList2.get(i);
                            bufferedWriter.write(song1[i]);bufferedWriter.flush();
                            flag1 = true;
                            }bufferedWriter.newLine();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        bufferedWriter.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                        if (flag1){
                            JOptionPane.showMessageDialog(null,"录入成功！");
                            dispose();
                            new Enter().setVisible(true);
                        }
                }
            }
        });
    }




    class TextFieldFocus implements FocusListener{
        @Override



        public void focusGained(FocusEvent e) {
            JTextField textField = (JTextField)e.getSource();
            JTextField textField1 =(JTextField)e.getSource();
            JTextField textField3 =(JTextField)e.getSource();
            JTextField textField4 =(JTextField)e.getSource();
            if (textField.equals("请输入书名：")){
                textField.setText("");


            }else if (textField1.equals("请输入书号：")){
                textField1.setText("");

            }else if (textField3.equals("请输入作者：")){
                textField3.setText("");

            }else{
                textField4.setText("");


            }

        }

        @Override
        public void focusLost(FocusEvent e) {

        }
    }


}
